<?php
/*
	File: index.php
		Also known as the "bootstrap".

	About: Author
		J. William McCarthy

	About: License
		<http://communit.as/docs/license>
*/

$ZZZZ_timer_on = @(boolean)$_GET['ZZZZtimer'];
if($ZZZZ_timer_on){
	$mtime = microtime();
	$mtime = explode(" ",$mtime);
	$mtime = $mtime[1] + $mtime[0];
	$ZZZZ_starttime = $mtime;
}

ini_set('display_errors', 0); 
$basepath =  substr($_SERVER['SCRIPT_FILENAME'] , 0 ,strrpos( $_SERVER['SCRIPT_FILENAME'], "/" ));
set_include_path(get_include_path() . PATH_SEPARATOR . $basepath);
$ip = $_SERVER['REMOTE_ADDR'];
try {
	require_once('header.php');
	// removed aliases
    
	if (!(boolean)$zf_config['application']['launched']) {
		$allowed_ips = explode(",", $zf_config['application']['allowed_ips']);
		if (!in_array($ip, $allowed_ips)) {
			header ("Location: " . $zf_config['application']['prelaunch_url']);
		}
	}
	
	if(!Zend_Registry::isRegistered("theme")){
		Zend_Registry::set("theme","default");	 
	}
	$theme = Zend_Registry::get("theme");
	$theme_url = "/themes/" . $theme;
	$default_url = "/themes/default";
	$theme_global_path = $basepath . $theme_url . "/global";	
	Zend_Registry::set('theme_global_path', $theme_global_path);	
	Zend_Registry::set('default_global_path', $basepath."/themes/default/global");		
	Zend_Registry::set('theme_url', $theme_url);
	Zend_Registry::set('default_url', $default_url);

	// Replace the ViewRenderer with Smarty as the default view 
	// args: template dir, smarty paths

    if ($isInstalled) {
        $smarty_config = new Zend_Config_Ini($config_file, 'smarty');    
    	$smarty_config_array = $smarty_config->config->toArray();   	
    } else {
    	
    	$smarty_view_compiles	= $basepath . "/tmp/view_compiles";
    	$smarty_cache_dir 		= $basepath . "/tmp/cache";
    	if(!file_exists($smarty_view_compiles)){
    		throw new Exception("DIR_MISSING Missing directory ".$smarty_view_compiles);
    	}
    	
    	if(!file_exists($smarty_cache_dir)){
    		throw new Exception("DIR_MISSING Missing directory ".$smarty_cache_dir);
    	}
    	
    	if(!is_writable($smarty_view_compiles)){
    		throw new Exception("CANT_WRITE Can't write to directory ".$smarty_view_compiles);
    	}
    	
    	if(!is_writable($smarty_cache_dir)){
    		throw new Exception("CANT_WRITE Can't write to directory ".$smarty_cache_dir);
    	}
    	
    	
		$smarty_config_array['compile_dir'] = $smarty_view_compiles;
    	$smarty_config_array['cache_dir'] 	= $smarty_cache_dir;
    }
    $smarty_config_array['plugins_dir'] 	= $smarty_plugins_dirs;

    $view = new App_View_Renderer(null, $smarty_config_array);   
    $view_renderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
    $view_renderer->setNoController(true)
                  ->setViewBasePathSpec($basepath . '/themes/' . $theme . '/modules/:module')
                  ->setViewScriptPathSpec(':controller/:action.:suffix')
                  ->setViewScriptPathNoControllerSpec(':action.:suffix')
                  ->setViewSuffix('tpl');                      
    Zend_Controller_Action_HelperBroker::addHelper($view_renderer);
	
		
    // Setup controller
    $front = Zend_Controller_Front::getInstance();
    $front->addModuleDirectory($module_dir);
    $front->throwExceptions(true);
    $front->setParam('noViewRenderer', false);
    
    if($isInstalled){
	    // add ACL plugin
		$front->registerPlugin(new AclPlugin);    
	    $app_plugin = new App_Plugin();
	    $app_plugin->doAction('bootstrap', array('front_controller' => $front));
	} else {
		$front->registerPlugin(new InstallPlugin);
	}

	if(file_exists($routes_file)){
		$routes = new Zend_Config_Ini($routes_file, 'default');
		$router = new Zend_Controller_Router_Rewrite();
		$router->addConfig($routes, 'routes');
		$front->setRouter($router);		
	}
	
    $front->dispatch();

} catch(Zend_Db_Statement_Exception $ex){
		$logger->report("Database error", $ex, Zend_Log::EMERG);
		header("Location: /errordocuments/error_DB.html");

} catch(Exception $ex) {
    	
	if($ip == "127.0.0.1"){	
		d($ex->getMessage());
		dd($ex);
	} else {
		$ex_type = trim(substr($ex->getMessage(),0,strpos($ex->getMessage()," ")));
		switch ($ex_type){
			case "MISSING_LIBS":
				header("Location: /errordocuments/error_LIBS.html");
			break;
			
			case "CANT_WRITE":
				header("Location: /errordocuments/error_CANTWRITE.html");
			break;
			
			case "DIR_MISSING":
				header("Location: /errordocuments/error_DIRMISSING.html");
			break;
			
			default:
				header("Location: /errordocuments/error_500.html");
				$logger->report("Frontcontroller Error", $ex, Zend_Log::EMERG);
			break;
		}
		
	}
}
if($ZZZZ_timer_on){
	$mtime = microtime();
	$mtime = explode(" ",$mtime);
	$mtime = $mtime[1] + $mtime[0];
	$endtime = $mtime;
	$totaltime = ($endtime - $ZZZZ_starttime);   
	echo("\n\n<!-- Page loaded in ".$totaltime." seconds -->");
}
